﻿using IOA.MES.Business;
using IOA.MES.DataAccess.Enum;
using IOA.MES.DataAccess.VModel;
using IOA.MES.Log.DataAccess.Enum;
using IOA.MES.Web.App_Start;
using IOA.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;

namespace IOA.MES.Web
{
    public class BaseEnv
    {
        /// <summary>
        /// 设备类型
        /// </summary>
        public static DeviceType DeviceType
        {
            get
            {
                try
                {
                    var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
                    if (userAgent.Contains("windows"))
                    {
                        return DeviceType.Windows;
                    }
                    else if (userAgent.Contains("mac"))
                    {
                        return DeviceType.MacOS;
                    }
                    else if (userAgent.Contains("android"))
                    {
                        return DeviceType.Android;
                    }
                    else if (userAgent.Contains("ios"))
                    {
                        return DeviceType.iOS;
                    }
                    else
                    {
                        return DeviceType.Other;
                    }
                }
                catch (Exception ex)
                {
                    return DeviceType.Other;
                }
            }
        }

        /// <summary>
        /// 客户端IP
        /// </summary>
        public static string ClientIP
        {
            get
            {
                string ipv4 = String.Empty,
                    clientIp = HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null ? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                foreach (var ip in Dns.GetHostAddresses(clientIp))
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        ipv4 = ip.ToString();
                        break;
                    }
                }
                if (ipv4 != string.Empty)
                {
                    return ipv4;
                }
                foreach (var ip in Dns.GetHostEntry(clientIp).AddressList)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        ipv4 = ip.ToString();
                        break;
                    }
                }

                return ipv4;
            }
        }

        /// <summary>
        /// 当前登录用户ID
        /// </summary>
        public static int UserId
        {
            get
            {
                try
                {
                    return Convert.ToInt32(HttpContext.Current.User.Identity.Name);
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 当前登录用户ID
        /// </summary>
        public static VUser User
        {
            get
            {
                if (UserId <= 0)
                {
                    return null;
                }
                return new UserMgr().GetUser(UserId);
            }
        }

        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="password">密码明文</param>
        /// <returns>密码密文</returns>
        public static string EncripyPassword(string password)
        {
            return Md5Utils.Md5Encrypt($"https://mes.ioa.tech/pwdencripy_{password}");
        }

        /// <summary>
        /// 错误代码
        /// </summary>
        public static List<LangPackage> LangPackages { get; set; }
    }
}